#include "log.h"

void log(int fd, const char *fmt, ...){
    char buffer[LOG_BUFFER_MAX];
    va_list arg;
    va_start(arg, fmt);
    vsprintf(buffer, fmt, arg);
    va_end(arg);
    write(fd, buffer, strlen(buffer));
}

void log(const char *fmt, ...){
    char buffer[LOG_BUFFER_MAX];
    va_list arg;
    va_start(arg, fmt);
    vsprintf(buffer, fmt, arg);
    va_end(arg);
    write(fileno(stdout), buffer, strlen(buffer));
}

void log(FILE* archivo, const char *fmt, ...){
    int fd = fileno(archivo);
    char buffer[LOG_BUFFER_MAX];
    va_list arg;
    va_start(arg, fmt);
    vsprintf(buffer, fmt, arg);
    va_end(arg);
    write(fd, buffer, strlen(buffer));
}

void clog(int frente, int fondo, const char * fmt, ...){
    char buffer[LOG_BUFFER_MAX];
    sprintf(buffer,"\x1B[4%i;1;3%im", fondo,frente);
    va_list arg;
    va_start(arg, fmt);
    vsprintf(buffer+10, fmt, arg);
    va_end(arg);
    strcat(buffer, "\x1B[0m");
    write(fileno(stdout), buffer, strlen(buffer));
}

void clog(int color, const char * fmt, ...){
    char buffer[LOG_BUFFER_MAX];
    sprintf(buffer,"\x1B[4%i;1;3%im", NEGRO, color);
    va_list arg;
    va_start(arg, fmt);
    vsprintf(buffer+10, fmt, arg);
    va_end(arg);
    strcat(buffer, "\x1B[0m");
    write(fileno(stdout), buffer, strlen(buffer));
}
